import vehicleVersionApi from '@/api/modules/software/vehicleVersion'
import commonFun from '@/utils/common'
const initQueryParams = {
  // 车型ID
  modelId: undefined,
  // 车辆版本名称
  name: undefined,
  vehicleVersionId: undefined,
  createBy: undefined
}
const state = {
  // 列表查询条件
  queryParams: {
    ...initQueryParams
  },
  rollbackOptions: [{
    id: 1,
    name: 'rollbackYes',
    value: true
  }, {
    id: 2,
    name: 'rollbackNo',
    value: false
  }]

}
const mutations = {
  resetVehicleVersionQueryParams (state) {
    // eslint-disable-next-line no-param-reassign
    state.queryParams = {
      ...initQueryParams
    }
  }
}

const actions = {
  // 添加
  // eslint-disable-next-line no-unused-vars
  addVehicleVersionData ({ commit }, data) {
    return new Promise((resolve, reject) => {
      vehicleVersionApi.addVehicleVersionData(data, data.versionType).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 编辑
  // eslint-disable-next-line no-unused-vars
  editVehicleVersionData ({ commit }, data) {
    return new Promise((resolve, reject) => {
      vehicleVersionApi.editVehicleVersionData(data, data.versionType).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 详情
  // eslint-disable-next-line no-unused-vars
  getVehicleVersionDetailData ({ commit }, data) {
    return new Promise((resolve, reject) => {
      vehicleVersionApi.getVehicleVersionDetailById(data.id, data.versionType).then(res => {
        if (!data.isOnlyEcuVersion) {
          // 是否只要零件版本配置列表
          resolve(res)
          return
        }
        const info = res.data
        let result = []
        if (info) {
          result = info.ecuVersions.map((item, index) => {
            return {
              ecuId: item.ecuId || index,
              partName: item.ecu ? item.ecu.name : '-',
              showEcuId: item.ecuModuleId || '-',
              softwareCode: item.softwareCode || '-',
              versionName: item.version ? item.version.name : '-',
              configVersionName: item.version ? item.version.configVersionName : '-',
              patchPackageCount: item.version ? item.version.patchPackageCount : '-',
              wholePackageCount: item.version ? item.version.wholePackageCount : '-',
              groupNo: item.groupNo || '-',
              groupRollback: item.groupRollback,
              groupUpgradeNo: item.groupUpgradeNo || '-',
              versionType: item.version.versionType,
              version: item.version,
              releaseNotes: item.version ? {
                description: item.version.description,
                functionAddition: item.version.functionAddition,
                functionOptimization: item.version.functionOptimization,
                acknowledgements: item.version.acknowledgements
              } : null
            }
          })
        }
        console.log(result, 'result')
        resolve(result)
      }).catch(err => {
        reject(err)
      })
    })
  }
}

export default {
  state,
  mutations,
  actions
}
